import axios from 'axios'
// import {getCookieToken} from "@/utils/auth";
// import {getCookieToken} from "./auth"
import store from "@/store";

const TimeOut = 150000000 // 定义超时时间
// 创建了一个新的axios实例
const service = axios.create({
    url: "",
    // baseURL: "http://portal-m2i-system-cluster01.hithium.com",
    timeout: TimeOut, // 超时时间
    headers: {"Content-Type": "application/json"},
})

// 添加请求拦截器
service.interceptors.request.use(function (config) {
    // let token = getCookieToken()
    let token = store.state.token
    console.log('config=>', config)
    // let token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb3VudHJ5IjoiIiwiY3JlYXRpb25UaW1lIjoxNzAzNDc1OTA3LCJleHAiOjE3MDM0Nzk1MDcsImZpcnN0TmFtZSI6ImFkbWluIiwiaWF0IjoxNzAzNDc1OTA3LCJpZCI6IjY2ZDFiNmYwLWJhNTUtNDlmNi04YjJjLTE5NDE5ZTRlMzIyMiIsImlzcyI6Indpc2UtcGFhcyIsImxhc3RNb2RpZmllZFRpbWUiOjE2OTUxMDAyOTUsImxhc3ROYW1lIjoiYWRtaW4iLCJyZWZyZXNoVG9rZW4iOiJjMDI1ODlmZi02MTU1LTQxYjYtYTk2Yy02NGRmNmY4OTczNDkiLCJzdGF0dXMiOiIiLCJ1c2VybmFtZSI6ImFkbWluQGFkdmFudGVjaC5jb20ifQ.QzivwSQQQSSPUwLiJsLHNgIP8yoBOT9_5r2aeHay8WM'
    if (token) {
        config.headers['Authorization'] = `Bearer ${token}`
        config.headers['org-id'] = 1
    }
    return config
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
})

// 添加响应拦截器
service.interceptors.response.use((response) => {
    if (response.headers && response.headers['content-disposition']) {
        localStorage.setItem("fileName", response.headers['content-disposition'].split(";")[1].split("=")[1])
    }
    console.log('response=>', response)
    const {data, status, statusText} = response
    // const res = data
    if (status) {
        return data
    } else {
        return Promise.reject(statusText || '数据处理失败')
    }
    // return response
    // 对响应数据做点什么
}, function (error) {
    this.$message({
        message: '请稍后重试',
        type: 'warning'
    });
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么

    return Promise.reject(error)
})

// function TimeOuts () {
//     const timer = Date.now()
//     return (timer - getDate()) / 1000 > TimeOut
// }
export default service
